package com.tingyuge.think.in.elasticsearch.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/11/16
 */
@Configuration
@RequiredArgsConstructor
@Slf4j
public class DynamicIndexService {

    private final RestHighLevelClient restHighLevelClient;


    public void createSpushIndexForSave(){


//        restHighLevelClient.
//        restHighLevelClient.search()
    }

    public boolean existDynamicIndex(String indexName){
        String indexSuffix = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH"));
        indexName = indexName+indexSuffix;
        GetIndexRequest getIndexRequest = new GetIndexRequest("spush-" + indexSuffix);
        try {
            boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
            log.info("查询索引 {} 是否存在 返回结果: {}",indexName,exists);
            return exists;
        } catch (IOException e) {
            log.error("查询索引 {} 是否存在异常,",indexName,e);
            return false;
        }
    }

    public boolean existIndex(String indexName){
        GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
        try {
            boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
            log.info("查询索引 {} 是否存在 返回结果: {}",indexName,exists);
            return exists;
        } catch (IOException e) {
            log.error("查询索引 {} 是否存在异常,",indexName,e);
            return false;
        }
    }

    public boolean createDynamicIndex(String indexName) {
        String indexSuffix = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH"));
        indexName = indexName+"-"+indexSuffix;
        String mapping = "{\n" +
                "      \"properties\" : {\n" +
                "        \"content\" : {\n" +
                "          \"type\" : \"text\",\n" +
                "          \"analyzer\" : \"ik_max_word\"\n" +
                "        },\n" +
                "        \"mobile\" : {\n" +
                "          \"type\" : \"text\",\n" +
                "          \"analyzer\" : \"keyword\"\n" +
                "        },\n" +
                "        \"name\" : {\n" +
                "          \"type\" : \"text\",\n" +
                "          \"analyzer\" : \"ik_max_word\"\n" +
                "        }\n" +
                "      }\n" +
                "    }";
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        createIndexRequest.settings(Settings.builder()
                .put("number_of_shards", "1")
                .put("number_of_replicas","0")
                .build());
        createIndexRequest.mapping( mapping, XContentType.JSON);
        IndicesClient indices = restHighLevelClient.indices();
        boolean resultCreate = false;
        try {
            CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
            //得到响应
            resultCreate = createIndexResponse.isAcknowledged();
            log.info("创建动态索引: {}",resultCreate);
        } catch (Exception e) {
            log.error("创建 {} 索引异常",indexName,e);
            return resultCreate;
        }
        return resultCreate;
    }

}
